//https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items?start=0&count=6&for_mobile=1&ck=sew4;
// const getFilms = function () {
//   var p = new Promise(function (resolve, reject) {
//     wx.request({
//       url: 'https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items',
//       data: {
//         start: 0,
//         count: 6,
//         for_mobile: 1,
//         ck: "sew4"
//       },
//       success: resolve,
//       fail: reject
//     });
//   });
//   return p;
// };

// 模块化处理：将项目中所有的请求进行统一的管理
const URLS={
  hotUrl:"https://m.douban.com/rexxar/api/v2/subject_collection/movie_showing/items",
  latestUrl:"https://m.douban.com/rexxar/api/v2/subject_collection/movie_latest/items",
  searchUrl:"https://m.douban.com/rexxar/api/v2/search"
};

const ajaxRequest = function (options = {}) {
  return new Promise(function (resolve, reject) {
    options.success = resolve;
    options.fail = reject;
    wx.request(options)
  });
};
//搜索电影
const searchFilm=function(keywords=""){
  return ajaxRequest({
    url:URLS.searchUrl,
    data:{
      q:keywords
    }
  });
};


//加载热门电影
const loadHotFilms = function (start=0,count=6) {
  return ajaxRequest({
    url:URLS.hotUrl,
    data: {
      start: start,
      count: count,
      for_mobile: 1,
      ck: "sew4"
    }
  });
};

//加载近期热门电影
const loadLatestFilms = function (start=0,count=6) {
  return ajaxRequest({
    url:URLS.latestUrl,
    data: {
      start: start,
      count: count,
      for_mobile: 1,
      ck: "sew4"
    }
  });
};




module.exports = {
  loadHotFilms: loadHotFilms,
  loadLatestFilms:loadLatestFilms,
  searchFilm:searchFilm
};